package com.fare.taluo.application.impl.crawl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.filter.Filter;
import org.foreveross.hbase.client.HbaseStaticHistoryHbaseHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.application.crawl.GetNoFlightInfoLinesApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.db.hbase.HBaseRowModel;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.crawl.statistics.StaticHistoryHbaseVO;
@Transactional
@Scope("singleton")
@Service("getNoFlightInfoLinesApplication")
public class GetNoFlightInfoLinesApplicationImpl implements GetNoFlightInfoLinesApplication{

	@Resource(name ="sourceApplication")
	private SourceApplication sourceApplication;
	
	@Override
	public List<StaticHistoryHbaseVO> getEntityList(String grabDateStr) {
		//根据条件查询Hbase
		//000010012014101400499558
		List<Filter> filterList = new ArrayList<Filter>();
		List<StaticHistoryHbaseVO> voList=new ArrayList<StaticHistoryHbaseVO>();
		String startKey="",endKey="";//000010122014101610099997
		List<SourceEntityVO> sourceList =sourceApplication.findAllSourceEntity();
		try {
			List<HBaseRowModel> rowModels=new ArrayList<HBaseRowModel>();
			for(SourceEntityVO vo:sourceList){
				String sourceId=(vo.getId()>100?vo.getId()+"":(vo.getId()>10?"0"+vo.getId():"00"+vo.getId()));
				for(int i=6;i<10;i++){//没有航班信息的状态分别为6，7
//					ResultScanner scan = HBaseHelper.getRecords("CRAWL_DETAIL_INFO", "000010122014101610099997","000010122014101610099997",new String[]{"planeinfo","agentInfos"},filterList);
					startKey=StringUtils.rightPad("00001"+sourceId+grabDateStr.replaceAll("-", "")+"10"+i, 24, "0");
					endKey=StringUtils.rightPad("00001"+sourceId+grabDateStr.replaceAll("-", "")+"10"+i, 24, "Z");
					List<HBaseRowModel> rowModel = InstanceFactory.getInstance(com.fare.taluo.db.hbase.HbaseManager.class).getMultRecord("CRAWL_DETAIL_INFO", startKey, endKey);
					if(rowModel.size()>0){
						rowModels.addAll(rowModel);
					}
				}
			}
			
			if(rowModels.size()>0)
				voList=HbaseStaticHistoryHbaseHelper.modelTranEntity(rowModels);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		return voList;
	}

}
